use mpost_panel, clear 
gsort cl_b fundid age
	by cl_b fundid: gen smt= _n
	egen si_quin4 = xtile(soft_info) if smt==1, nq(4) //fix 
drop if smt>180
	gen logt= log(smt)
by cl_b fundid: replace si_quin4= si_quin4[_n-1] if smt!=1  
 
global indep1 "xret_style_nr"
global tim "logt"

replace ${indep1}= ${indep1}*100 // returns in bps
gen admxtim= logt*eadm // admitted fund decay
// gen HighInten = (si_quin4>2)+1
gen HighInten = (4.si_quin4)+1


* compute forward averages for PST(2015 regressions) iv regressions
local data ${indep1} laum ${tim} admxtim
foreach var in `data' {
	gen `var'_tb = `var' if xret_style_nr!=.
	bys cl_b fundid: replace `var'_tb = sum(`var'_tb) / sum(`var'_tb < .)
	replace `var'_tb= . if `var'==.
}

replace ${indep1}_tb= ${indep1}- ${indep1}_tb
replace laum_tb = laum- laum_tb
replace ${tim}_tb= ${tim}- ${tim}_tb
replace admxtim_tb= admxtim- admxtim_tb

replace laum_bb = laum-laum_bb // backwards demeaned variables (computed in "Primary Dataset Generation.do")


cap drop alfa*
eststo clear

eststo A1: reghdfe ${indep1} ${tim} admxtim, nocons absorb(fundid) vce(cl fundid) // regular fixed effect regression
gen alfa = ${indep1}- _b[${tim}]*${tim}- _b[admxtim]*admxtim
ttest alfa, by(eadm)
	scalar alfa_up = r(mu_2)
	estadd scalar alfa_up = r(mu_2)
	scalar alfa_down = r(mu_1) 
	estadd scalar alfa_down = r(mu_1)
	estadd scalar pval= r(p)

	scalar alfa_net= alfa_up-alfa_down
	scalar tim_neutral= (exp(alfa_net))^(-1/_b[admxtim]) // post-dd month where selected/unselected funds have same returns
	estadd scalar alfa_cum = (alfa_net-_b[admxtim])*(tim_neutral-1)+_b[admxtim]*(tim_neutral*log(tim_neutral)-tim_neutral+1) // cumulative excess returns
		
	estout, stats(alfa_up alfa_down pval alfa_cum tim_neutral)


	
eststo A2: reghdfe ${indep1} ${tim} admxtim, nocons absorb(fundid calendar_month) vce(cl fundid) // add time fixed effects, gut check
gen alfa2 = ${indep2}- _b[${tim}]*${tim}- _b[admxtim]*admxtim // if smt<12
ttest alfa2, by(eadm)
	scalar alfa_up = r(mu_2)
	estadd scalar alfa_up = r(mu_2)
	scalar alfa_down = r(mu_1) 
	estadd scalar alfa_down = r(mu_1)
	estadd scalar pval= r(p)

	scalar alfa_net= alfa_up-alfa_down
	scalar tim_neutral= (exp(alfa_net))^(-1/_b[admxtim]) // post-dd month where selected/unselected funds have same returns
	estadd scalar alfa_cum = (alfa_net-_b[admxtim])*(tim_neutral-1)+_b[admxtim]*(tim_neutral*log(tim_neutral)-tim_neutral+1) // cumulative excess returns
		
	estout, stats(alfa_up alfa_down pval alfa_cum)


	
eststo A3: reghdfe ${indep1} ${tim} i.HighInten#c.${tim}, nocons absorb(fundid) vce(cl fundid)
gen alfa3 = ${indep1}- _b[${tim}]+ _b[2.HighInten#c.${tim}]*2.HighInten*c.${tim}  if smt<24
global cond "HighInten==2" // analyze extreme portfolios
global opp_cond "HighInten==1"
capture drop temp
gen temp= . // indicator for long-short portfolios
replace temp= 0 if ${opp_cond}
replace temp= 1 if ${cond}
ttest alfa3, by(temp) // assumes A1 is true model
	estadd scalar alfa_up= r(mu_2)
	estadd scalar alfa_down = r(mu_1) 
	estadd scalar pval= r(p)
	
	sum alfa3 if ${cond}
		scalar alpha_adm= r(mean)
	sum alfa3 if ${opp_cond}
		scalar alfa_nadm= r(mean)
		

	scalar alfa_net= alfa_up-alfa_down
	scalar tim_neutral= (exp(alfa_net))^(-1/2.HighInten) // post-dd month where selected/unselected funds have same returns
	estadd scalar alfa_cum = (alfa_net-_b[2.HighInten#c.${tim}])*(tim_neutral-1)+_b[2.HighInten#c.${tim}]*(tim_neutral*log(tim_neutral)-tim_neutral+1) // cumulative excess returns

	estout, stats(alfa_up alfa_down pval alfa_cum)

replace ${tim}=${tim}_tb	
eststo A4: ivregress 2sls ${indep1}_tb (laum_tb = laum_bb), nocons vce(cl fundid)
eststo A5: ivregress 2sls ${indep1}_tb ${tim} (laum_tb = laum_bb), nocons vce(cl fundid)
eststo A6: ivregress 2sls ${indep1}_tb ${tim} admxtim (laum_tb = laum_bb), nocons vce(cl fundid)



global ordering "${tim} admxtim laum_tb"
label var ${tim} "Log(t)"
label var admxtim "\(\mathbb{I}_{sel} \times\) Log(t)"
label var laum_tb "\(\text{Log(AUM)}_{t-1}\)"

global indicateFEs "Manager FE &\multicolumn{1}{c}{Yes}&\multicolumn{1}{c}{Yes}&\multicolumn{1}{c}{Yes}&\multicolumn{1}{c}{IV}&\multicolumn{1}{c}{IV}&\multicolumn{1}{c}{IV}\\"
global indicateFEs "${indicateFEs} &\multicolumn{1}{c}{No}&\multicolumn{1}{c}{Yes}&\multicolumn{1}{c}{No}&\multicolumn{1}{c}{No}&\multicolumn{1}{c}{No}&\multicolumn{1}{c}{No}\\"


esttab A1 A2 A3 A4 A5 A6 using "${output}\Table6${sfx}.tex", replace frag plain nodiscrete nobaselevels order(${ordering}) ///
star(* 0.10 ** 0.05 *** 0.01) collabels(none) mlabels(none) nonotes eqlabel(none) num label drop(0.calendar_month 0.fundid _cons c1, relax) subs("\_" "_")  ///
stats(N alfa_up alfa_down pval alfa_cum, fmt(%15.0fc 3 3 3 3) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" ///
"\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"Observations"' `"\addlinespace \(a: \overline{R}_1 (\mathbb{I}=1) \)"' `"\(b: \overline{R}_1 (\mathbb{I}=0) \)"' ///
`"\(P_1(a > b)\)"' `"\addlinespace \(\int^T_1 \overline{R}_{\tau} (a - b) \)"')) cells("b(fmt(3)star)" "t(fmt(2)par([ ]))") varlabels(, elist(1.TwoByTwo#c.${tim} \addlinespace\addlinespace)) ///
preh(\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{@M}{d{3.6}}}\toprule ) ///
posth(\cmidrule(lr){2-7}\addlinespace) ///
pref(\addlinespace ${indicateFEs}) postf(\bottomrule\end{tabular})
